package com.gsafety.library.Widgets;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gsafety.library.Exts.Ext;
import com.gsafety.library.R;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;

/**
 * 刷新布局，底部控件
 */
public class RefreshFootView extends LinearLayout implements RefreshFooter
{

    private ImageView loading_img;
    private TextView tip;
    private ValueAnimator anim_loading;
    private boolean mNoMoreData = false;

    private String LOADING_TEXT = "正在加载..";
    private String LOADING_OVER_TEXT = "我是有底线的 ~ ";

    public RefreshFootView(Context context)
    {
        this(context, null);
    }

    public RefreshFootView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public RefreshFootView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.layout_default_foot_view, this, true);
        loading_img = view.findViewById(R.id.loading_img);
        tip = view.findViewById(R.id.tip);

        anim_loading = ObjectAnimator.ofFloat(loading_img, "rotation", 0f, 360f);
        anim_loading.setDuration(1000);
        anim_loading.setInterpolator(new LinearInterpolator());
        anim_loading.setRepeatCount(-1);

        loading_img.setRotationX(0.5f);
        loading_img.setRotationY(0.5f);
    }


    @Override
    public boolean setNoMoreData(boolean noMoreData)
    {
        if (mNoMoreData != noMoreData)
        {
            mNoMoreData = noMoreData;
            if (noMoreData)
            {
                tip.setText(LOADING_OVER_TEXT);
                Ext.gone(loading_img);
            } else
            {
                tip.setText(LOADING_TEXT);
                Ext.show(loading_img);
            }
        }
        return true;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle()
    {
        return SpinnerStyle.Translate;
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success)
    {
        return 500;
    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight)
    {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax)
    {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight)
    {

    }

    @NonNull
    @Override
    public View getView()
    {
        return this;
    }

    @Override
    public void setPrimaryColors(int... colors)
    {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight)
    {

    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState)
    {
        if (newState == RefreshState.PullUpToLoad)
        {
            if (!anim_loading.isRunning())
            {
                anim_loading.start();
            }
        } else if (newState == RefreshState.None)
        {
            anim_loading.cancel();

        }
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight)
    {

    }

    @Override
    public boolean isSupportHorizontalDrag()
    {
        return false;
    }
}
